package com.hzhk.happyrent.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.card.BankHotModel;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.adapter.card.CardYuyueAdapter;
import com.hzhk.happyrent.ui.view.listview.MaiTianListview;
import com.tamic.novate.Throwable;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class YuyueCardActivity extends Activity implements MaiTianListview.OnMaiTianRefreshListener {

    @BindView(R.id.activity_titlebar_left)
    ImageView activityTitlebarLeft;
    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.activity_titlebar_right)
    ImageView activityTitlebarRight;
    @BindView(R.id.lv_yuyue_card)
    MaiTianListview lvYuyueCard;
    @BindView(R.id.card_yuyue_bank)
    TextView cardYuyueBank;
    @BindView(R.id.card_yuyue_commit)
    TextView cardYuyueCommit;
    private CardYuyueAdapter cardYuyueAdapter;
    private BankHotModel bankHotModel;
    private String id;
    private String bankName;
    private String imageUil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yuyue_card);
        ButterKnife.bind(this);
        initView();
        initData();
        initEvent();
    }

    private void initEvent() {
        cardYuyueCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (id != null && id.length() != 0) {
                    Intent intent = new Intent(YuyueCardActivity.this, BankCardItemActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("id", id);
                    bundle.putString("bankName", bankName);
                    bundle.putString("imageUil", imageUil);
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    Toast.makeText(YuyueCardActivity.this, "请先选择预约银行", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    private void initData() {
        Map<String, Object> map = new HashMap<>();

        HttpManager.getInstance().get(API.BANK_HOT, map, new MyStringResponse(this) {
            @Override
            public void onError(Object tag, Throwable e) {
//                Toast.makeText(YuyueCardActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }

            @Override
            public void onNext(Object tag, String response) {
                bankHotModel = new Gson().fromJson(response, BankHotModel.class);
                cardYuyueAdapter.setList(bankHotModel.getData().getBank());
            }
        });

    }

    private void initView() {
        activityTitlebarCenter.setText("预约办卡");
        activityTitlebarLeft.setImageResource(R.drawable.back);
        lvYuyueCard.setOnMaiTianRefreshListener(this);
        cardYuyueAdapter = new CardYuyueAdapter(this);
        lvYuyueCard.setAdapter(cardYuyueAdapter);
        lvYuyueCard.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                cardYuyueBank.setText(bankHotModel.getData().getBank().get(i - 1).getTitle());
                cardYuyueCommit.setBackgroundColor(getResources().getColor(R.color.tab_red));
                id = bankHotModel.getData().getBank().get(i - 1).getId();
                bankName = bankHotModel.getData().getBank().get(i - 1).getTitle();

                imageUil = bankHotModel.getData().getBank().get(i - 1).getImage();


            }
        });

    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {

    }

    @OnClick(R.id.activity_titlebar_left)
    public void onViewClicked() {
        finish();
    }

   /* @OnClick({R.id.activity_titlebar_left, R.id.activity_titlebar_center, R.id.activity_titlebar_right, R.id.lv_yuyue_card})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_titlebar_left:
                break;
            case R.id.activity_titlebar_center:
                break;
            case R.id.activity_titlebar_right:
                break;
            case R.id.lv_yuyue_card:
                break;
        }
    }*/
}
